<?php
class base extends Controller{

	public function init(){
		parent::init();

		$this->admin_id = $this->getUserId();
		// $this->admin_id = $this->get_admin_id();
		
		$this->m_user = get_cookie('m_user');
        $this->m_pass = get_cookie('m_pass');
        $this->remember = get_cookie('remember');

        $this->module = arg('m');
		$this->controller = arg('c');
		$this->action = arg('a');
		$this->routeUrl = $this->module.'/'.$this->controller;
		// 
		$this->fmdo = arg('do');
		$this->tab = arg('tab');
		$this->doids = arg('doids');
		$this->keyword = arg('keyword');
		$this->sort = arg('sort');
		$this->list_order = arg('list_order');
		$this->time_s = arg('time_s');
		$this->time_e = arg('time_e');
		$this->status = arg('status');

		$this->sexRadio = array('1'=>'男','0'=>'女');

		$this->moshiRadio = array('1'=>'自由接单','2'=>'自动派单','3'=>'循环派单');
		$this->userLevels = array('1'=>'报修员','2'=>'报修组长','3'=>'维修师傅','4'=>'维修组长');
		$this->orderStatus = array('1'=>'新订单','2'=>'已派单','3'=>'已接单','4'=>'维修中','5'=>'已完成','6'=>'已评价','7'=>'已结算','99'=>'已取消');

		// $this->city_id = arg('game_id');
		// if($this->city_id){
		// 	$this->game = D('game_list')->get_game_info($this->city_id);
		// 	$this->cond_game = " and game_id={$this->city_id}";
		// }

		if($this->admin_id){
			$this->admin = D('company_admin')->findById($this->admin_id);
			$this->company = D('company')->findById($this->admin['company_id']);
			$this->company_setting = unserialize($this->company['setting']);

			$this->cond_base = " and company_id={$this->company['id']}";
			$this->cond_city = " and (city_id IN ({$this->company['city_ids']}))";
			$this->cond_area = " and (city_id IN ({$this->company['city_ids']}) or area_id IN ({$this->company['city_ids']}))";
		}

		$this->setting = D('sys_setting')->get_setting();
	}

	public function checkLogin(){
		if($this->routeUrl != "api/login"){
			$this->admin_id = $this->getUserId();
			if(!$this->admin_id){
				//  $this->redirect("/login/index");
				$this->throwError('请先登录',-1);
			}
		}
	}

	public function getUserId()
    {
		$token = $_SERVER['HTTP_TOKEN'] ?? arg('token');
		if($token){
			$admin = D('company_admin')->get_user_by_token($token);
			if($admin){
				return $admin['id'];
			}
		}
		return false;
	}

	public function getUser($is_force = true)
    {
		$token = $_SERVER['HTTP_TOKEN'] ?? arg('token');
		if(!$token){
			$is_force && $this->throwError('缺少必要的参数：token',-1);
		}
		$admin = D('company_admin')->get_user_by_token($token);
		if(!$admin){
			$is_force && $this->throwError('没有找到用户信息', -1);
            return false;
		}
		return $admin;
	}

	public function ajaxMsg($code=0, $msg='', $url='', $data=[]){
		header('Content-Type:application/json; charset=utf-8');
        return compact('code', 'msg', 'url', 'data');
    }


    public function redirect($url){
		$arr = explode('/',$url);
    	if(count($arr) == 3){
			$url = "/{$this->module}{$url}";
		}
		header("Location:" . $url);
		exit;
	}
	

	protected function renderJson($code = 1, $msg = '', $url = '', $data = [])
    {
        header('Content-Type:application/json; charset=utf-8');
        exit(json_encode(compact('code', 'msg', 'url', 'data')));
        // return compact('code', 'msg', 'url', 'data');
    }

    protected function renderSuccess($msg = 'success', $url = '', $data = [])
    {
        return $this->renderJson(1, $msg, $url, $data);
    }

    protected function renderError($msg = 'error', $url = '', $data = [])
    {
        return $this->renderJson(0, $msg, $url, $data);
	}
	
	protected function throwError($msg, $code = 0)
    {
        return $this->renderJson($code, $msg);
	}

    protected function postData($key)
    {
        return arg($key);
	}
	
	protected function formData($key)
    {
		$key = arg($key);
        return json_decode($key,true);
	}
	
	public static function err404($module, $controller, $action){
		header("HTTP/1.0 404 Not Found");
		exit;
	}

	protected function isAjax(){
		return true;
		if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
		  return true;
		}else{
		  return false;
		}
	  }
	
} 